﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using LPV_POS.Module;

namespace LPV_POS.mh_pos.mh_ban_hang
{
    public partial class mh_banhang_CT_Doi_Diem : DevExpress.XtraEditors.XtraForm
    {
        DateTime Ngay;
        string sMaTheVIP = "-1";
        int idiem = 0;
        string stmaPhieu = "";
        //editor: PL, date: 2012.08.10
        public static bool flag = false;
        public static int idiembandau = 0;
        public static int idiemdatru = 0;
        //end 2012.08.10
        
        string DM_KHUYENMAI_MA = "";
        public mh_banhang_CT_Doi_Diem(string _sMaTheVIP, DateTime _Ngay)
        {
            InitializeComponent();
            Ngay = _Ngay;
            sMaTheVIP = _sMaTheVIP;
            //editor: PL, date: 2012.08.10
            stmaPhieu = mh_banhang_CT.stmaPhieu_BH;
            flag = false;
            idiemdatru = 0;
            idiembandau = 0;
            //end 2012.08.10
        }

        private void mh_banhang_CT_Doi_Diem_Load(object sender, EventArgs e)
        {
            this.ShowData();
        }


        void ShowData()
        {
            string sSQL = "";
            thuvien othuvien = new thuvien();

            sSQL = "SELECT ISNULL((SELECT SUM(DIEM) FROM BH_PHIEU_LUU_DIEM WHERE DM_THE_CODE LIKE '" + sMaTheVIP + "'), 0) " +
                        "- ISNULL((SELECT SUM(DIEM) FROM BH_PHIEU_DOIDIEM_THANHMON WHERE DM_THE_CODE LIKE '" + sMaTheVIP + "'),0) " +
                        " - ISNULL((SELECT SUM(DIEM) FROM BH_PHIEU_DOIDIEM_TRUDIEM WHERE DM_THE_CODE LIKE '" + sMaTheVIP + "'),0)";     //editor: PL, date: 2012.08.10
            DataTable tbDIEM = othuvien.SP_SelectDataTable("SP_EXECUTESQL",
                                                            new string[] { "@sSQL" },
                                                            new object[] { sSQL });

            if (tbDIEM.Rows.Count > 0)
                int.TryParse(tbDIEM.Rows[0][0] + "", out idiem);
            lbl.Text = "Tổng điểm hiện nay là: " + idiem.ToString("N0");
            
            ///////////////////////////////////////////////////

            if (sMaTheVIP == "")
                sMaTheVIP = "-1";

            sSQL = "SELECT k.*, kh.TEN_COT_KM, TONG_KM = (CAST(" + idiem + "/THAMSO_1 AS INT) * THAMSO_2) " +
                    "FROM DM_KHUYENMAI k " +
                    "   INNER JOIN DM_KHUYENMAI_THE kt ON k.OBJID = kt.MAKHUYENMAI " +
                    "   LEFT JOIN DM_KHUYENMAI_HINHTHUC kh ON k.HINHTHUC_KM = kh.MA_HTKM " +
                    "WHERE k.HINHTHUC_KM IN('11', '12', '15') " +//                    "   AND KT.MATHE = " + sMaTheVIP + " " +
                    "   AND (KT.MATHE = '" + sMaTheVIP + "' OR (KT.MATHE = '0' AND kt.DM_NHOMKHACHHANG_MA IN (SELECT dt.DM_NHOMKHACHHANG_MA FROM DM_THE dt WHERE dt.CODE = '" + sMaTheVIP + "'))) " +
                    "   AND CONVERT(VARCHAR(10), TUNGAY,112) <= " + Ngay.ToString("yyyyMMdd") + " " +
                    "   AND " + Ngay.ToString("yyyyMMdd") + " <= CONVERT(VARCHAR(10), DENNGAY,112) ";

            DataTable tbKM = othuvien.SP_SelectDataTable("SP_EXECUTESQL",
                                                            new string[] { "@sSQL" },
                                                            new object[] { sSQL });
            grdKhuyenMai.DataSource = tbKM;
            if (tbKM.Rows.Count > 0)
            {
                col_tien.Caption = tbKM.Rows[0]["TEN_COT_KM"] + "";
                col_tien_TOTAL.Caption = "Tổng " + col_tien.Caption.Trim().ToLower();
            }

        }

        private void bton_thoat_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void btnDoiDiem_Click(object sender, EventArgs e)
        {
            
            //int iHinhThucKM = 0;//11: tang TIEN; 12: tang MON

            if (grvKhuyenMai.FocusedRowHandle >= 0)
            {
                DataRow dr = grvKhuyenMai.GetDataRow(grvKhuyenMai.FocusedRowHandle);
                float fdiemDM = 0;
                float.TryParse(dr["thamso_1"] + "", out fdiemDM);
                int islq = 0;

                //editor: PL, date: 2012.08.10
                int.TryParse(dr["thamso_2"] + "", out islq);
                if (idiem < fdiemDM)
                {
                    MessageBox_LPV.Show("Số điểm hiện có chưa đủ.");
                    return;
                }
                DM_KHUYENMAI_MA = dr["OBJID"] + "";

                int ifd = 0;
                float fdiemtru = 0;
                if (stmaPhieu == "")
                {
                    int.TryParse(fdiemDM.ToString(), out ifd);
                    int idiemdu = idiem % ifd;                      //điểm còn lại
                    int iqua = (idiem * islq) / ifd;                //số lượng quà
                    int idiemtru = ifd * iqua;                      //số điểm thực đã trừ
                    fdiemtru = 0;
                    float.TryParse(idiemtru.ToString(), out fdiemtru);

                    int kq = f_insertBH_PHIEU_TRUDIEM(sMaTheVIP, fdiemtru, "Tổng điểm trước: " + idiem.ToString());


                    string sSQL = "SELECT ISNULL((SELECT SUM(DIEM) FROM BH_PHIEU_DOIDIEM_TRUDIEM WHERE DM_THE_CODE LIKE '" + sMaTheVIP + "'),0)";
                    DataTable tbDIEM = new thuvien().SP_SelectDataTable("SP_EXECUTESQL",
                                                                    new string[] { "@sSQL" },
                                                                    new object[] { sSQL });

                    //int.TryParse(tbDIEM.Rows[0][0] + "", out idiemdatru);
                    //idiembandau = idiem;
                    flag = true;
                }

                else
                {
                     ifd = 0;
                    int.TryParse(fdiemDM.ToString(), out ifd);
                    int idiemdu1 = idiem % ifd;                      //điểm còn lại
                    int iqua1 = (idiem * islq) / ifd;                //số lượng quà
                    int idiemtru1 = ifd * iqua1;                      //số điểm thực đã trừ

                    idiemdatru = idiemtru1;
                    idiembandau = idiem;
                }
                //end -- 2012.08.10
                //int.TryParse(dr["hinhthuc_km"] + "", out iHinhThucKM);
            }
            else
                MessageBox_LPV.Show("Chọn chương trình khuyến mãi trong danh sách");

            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void grvKhuyenMai_Click(object sender, EventArgs e)
        {
            if (grv_hangHoa.RowCount == 1)
                this.ShowData_Detail();
        }

        #region "   ShowData_Detail"
        void ShowData_Detail()
        {
            string maKHUYENMAI = "";
            int iHinhThucKM = 0;//11: tang TIEN; 12: tang MON

            if (grvKhuyenMai.FocusedRowHandle >= 0)
            {
                DataRow dr = grvKhuyenMai.GetDataRow(grvKhuyenMai.FocusedRowHandle);
                maKHUYENMAI = dr["OBJID"] + "";
                int.TryParse(dr["hinhthuc_km"] + "", out iHinhThucKM);
                col_tien.Caption = dr["TEN_COT_KM"] + "";
            }

            string sSQL =
                            "SELECT KC.OBJID, KC.MAKM, KC.MAHHBAN, KC.MAHHTANG, KC.MAQUATANG, " +
                            "     KC.GIATRI_PHANTRAM, " +
                            "     TEN_HHBAN = H1.OBJNAME,  " +
                            "     TEN_HHTANG = H2.OBJNAME,  " +
                            "     TEN_QUATANG = KQ.TENQUATANG, " +
                            "     DVT1 = d1.OBJNAME, " +
                            "     DVT2 = d2.OBJNAME, " +
                            "     DVT3 = ISNULL(d3.OBJNAME, KQ.DVT) " +
                            "FROM DM_KHUYENMAI_CT KC  " +
                            "   LEFT JOIN DM_HH H1 ON KC.MAHHBAN = H1.OBJID " +
                            "   LEFT JOIN DM_HH H2 ON KC.MAHHTANG = H2.OBJID " +
                            "   LEFT JOIN DM_KHUYENMAI_QUATANG KQ ON KC.MAQUATANG = KQ.MAQUATANG " +
                            "   LEFT JOIN DM_DVT d1 ON d1.OBJID = H1.MADVT " +
                            "   LEFT JOIN DM_DVT d2 ON d2.OBJID = H2.MADVT " +
                            "   LEFT JOIN DM_DVT d3 ON d3.OBJID = KQ.DVT " +//9999
                            "WHERE KC.MAKM LIKE '" + maKHUYENMAI + "'";

            DataTable tbDM_KHUYENMAI_CT = new thuvien().SP_SelectDataTable("SP_EXECUTESQL", new string[] { "@sSQL" }, new object[] { sSQL });
            grd_hangHoa.DataSource = tbDM_KHUYENMAI_CT;
        }
        #endregion

        private void grvKhuyenMai_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            this.ShowData_Detail();
        }

        public string ShowForm()
        {
            this.ShowDialog();
            return DM_KHUYENMAI_MA;
        }
            
        //editor: PL, date: 2012.08.10
        private int f_insertBH_PHIEU_TRUDIEM(string stmakh, float fdiem, string stghichu)
        {
            thuvien tv = new thuvien();
            string[] s = new string[] { "@flag", "@DM_THE_CODE", "@DIEM", "@GHI_CHU" };
            object[] o = new object[] { 1, stmakh, fdiem, stghichu };
            int kq = tv.SP_Update("sp_BH_PHIEU_DOIDIEM_THANHMON", s, o);
            return kq;
        }
    }
}